class Solution
{
public:
    int maximizeSquareArea(int m, int n, vector<int> &hFences, vector<int> &vFences)
    {
        unordered_set<long long> hDists;
        unordered_set<long long> vDists;
        sort(hFences.begin(), hFences.end());
        sort(vFences.begin(), vFences.end());
        hFences.insert(hFences.begin(), 1);
        hFences.push_back(m);
        vFences.insert(vFences.begin(), 1);
        vFences.push_back(n);

        for (int i = 1; i < hFences.size(); i++)
        {
            for (int j = 0; j < i; j++)
            {
                hDists.insert(hFences[i] - hFences[j]);
            }
        }

        for (int i = 1; i < vFences.size(); i++)
        {
            for (int j = 0; j < i; j++)
            {
                vDists.insert(vFences[i] - vFences[j]);
            }
        }

        long long maxArea = -1;
        for (auto hDist : hDists)
        {
            if (!vDists.count(hDist))
            {
                continue;
            }
            maxArea = max(maxArea, hDist * hDist);
        }
        if (maxArea > 0)
        {
            maxArea %= 1000000007;
        }
        return maxArea;
    }
};